//
//  PKPlayListTableViewController.m
//  PianKe
//
//  Created by lanou on 15/12/25.
//  Copyright © 2015年 KFC. All rights reserved.
//

#import "PKPlayListTableViewController.h"
#import "PKPlayListTableViewCell.h"
#import "PKDetailModel.h"
#import "PKDownLoadManager.h"
@interface PKPlayListTableViewController ()


@property (assign, nonatomic) BOOL isDown;


@end

@implementation PKPlayListTableViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 注册cell (第二种写法)
    [self.tableView registerClass:[PKPlayListTableViewCell class] forCellReuseIdentifier:@"PKPlayListCell"];
    self.tableView.rowHeight = 55;

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.listArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PKPlayListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PKPlayListCell" forIndexPath:indexPath];
    
    PKDetailModel *model  = self.listArray[indexPath.row];
    cell.model = model;
    // 点击时的效果
    //  cell.selectionStyle = UITableViewCellSelectionStyleNone;
#pragma mark ---- 列表左侧绿色框框的效果
    if (model.isSelected == YES) {
        cell.isSelectLabel.alpha = 1;
    } else
        cell.isSelectLabel.alpha = 0;
    
    
#pragma mark ---- 点击下载音乐的操作
    // 点击下载按钮
    cell.downLoadButton.tag = 100 + indexPath.row;
    
    
    PKDownLoad *load = [[PKDownLoadManager shareManager] downLoadFrom:model];
    if (load == nil) {
        // 说明:下载列表中没有这个下载任务
        NSLog(@"没有进行下载");
        // 显示下载按钮 , 影藏进度的label
        
      [cell.downLoadButton addTarget:self action:@selector(downLoad:) forControlEvents:(UIControlEventTouchUpInside)];
        
    } else
    {   // 说明:这个model 正在下载列表中
        
        NSLog(@"说明model在下载队列里%ld",indexPath.row);
        
       // model.isDown = YES;
        
        // 显示label , 影藏下载按钮
        // load 就是这个单元格对应的下载任务
        if (load.downloadState == -1) {
            // -1说明在下载
            NSLog(@"说明load正在下载");
            // label显示百分比
            load.block = ^void(NSNumber *scale){
              
                NSString *title = [NSString stringWithFormat:@"%@",scale];
                [cell.downLoadButton setTitle:title forState:(UIControlStateNormal)];
                if ([title isEqualToString:@"1"]) {
                    
                    [cell.downLoadButton setTitle:@"已缓存" forState:(UIControlStateNormal)];
                }
                
            };
            
            
        }else if (load.downloadState == 1)
        {
            // 1说明下载已经完成
            [cell.downLoadButton setTitle:@"已缓存" forState:(UIControlStateNormal)];
            
        }
        else {
            // 0 label 显示暂停
            [cell.downLoadButton setTitle:@"暂停" forState:(UIControlStateNormal)];
        }
        
    }
    
    return cell;
}

#pragma mark ---- 实现下载按钮的方法
- (void)downLoad:(UIButton *)button
{
    PKDownLoad *downLoad = [[PKDownLoad alloc] initWithModel:_listArray[button.tag - 100]];
    [[PKDownLoadManager shareManager]addDownLoadToManager:downLoad];
            
    
    // 指定某几个单元格 刷新,重新走一遍cellForRow方法
    [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForItem:button.tag - 100 inSection:0]] withRowAnimation:(UITableViewRowAnimationTop)];
    
    
}


// 暂停
- (void)pauseDownLoad:(UIButton *)button
{
    PKDetailModel *model = _listArray[button.tag - 100];
    
    if (model.isDown == NO) {
        // 说明modle不在下载
        // 那么点击后,model开始下载
        // 根据被点击的按钮的tag来 算出这个按钮在第几个cell上,取到cell对应的model,然后去参加一个下载任务
        
        // 要进行判断,此时这个model是不是之前已经下载过了
        PKDownLoad *judge = [[PKDownLoadManager shareManager]downLoadFrom:_listArray[button.tag - 100]];
        if (judge.tempData != nil) {
            
            [judge goonDownLoad];
            
        } else
        {
            PKDownLoad *downLoad = [[PKDownLoad alloc] initWithModel:_listArray[button.tag - 100]];
            [[PKDownLoadManager shareManager]addDownLoadToManager:downLoad];
            
        }
        
    } else {
        
        // 说明点击按钮前,在下载
        // 那么点击过后,是暂停
        
        PKDownLoad *downLoad = [[PKDownLoadManager shareManager]downLoadFrom:_listArray[button.tag - 100]];
        [downLoad pauseDownLoad];
        
    }
    
    // 指定某几个单元格 刷新,重新走一遍cellForRow方法
    [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForItem:button.tag - 100 inSection:0]] withRowAnimation:(UITableViewRowAnimationTop)];
    
    
}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSInteger index = indexPath.row;
    
    if ( _changeDelegate && [_changeDelegate respondsToSelector:@selector(changeWithIndex:)]) {
        
        [_changeDelegate changeWithIndex:index];
        
    }
    
  
}





/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
